package android.medicalcloud.com.yiapp.base.net;

import android.medicalcloud.com.yiapp.base.exc.BaseException;
import android.medicalcloud.com.yiapp.base.net.lisener.HttpNetCallBack;
import android.medicalcloud.com.yiapp.base.net.lisener.NetCallback;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.MapUtils;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.os.AsyncTask;



import java.util.Map;

/**
 * Created by Fenix on 2016/3/18.
 */
public class HttpUrlConnectionManager  implements HttpNetCallBack {

    static  HttpUrlConnectionManager instance;

    public static HttpUrlConnectionManager getInstance() {
        if (instance==null){
            instance = new HttpUrlConnectionManager();
        }
        return instance;
    }

    @Override
    public void get(String url, Map<String, Object> params, long timeOut, boolean cach, NetCallback responesCallBack) throws BaseException {

    }

    @Override
    public void getWithHeader(String url, Map<String, Object> params, Map<String, String> header, long timeOut, boolean cach, NetCallback responesCallBack) throws BaseException {
        new NetGetHeaderAstask( url, params,  header,  timeOut,  cach,  responesCallBack).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"");
    }

    @Override
    public void post(String url, Map<String, Object> params, long timeOut, boolean cach, NetCallback responesCallBack) throws BaseException {

    }

    @Override
    public void postWithHeader(String url, Map<String, Object> params, Map<String, String> header, long timeOut, boolean cach, NetCallback responesCallBack) throws BaseException {
        new NetPostHeaderAstask( url, params,  header,  timeOut,  cach,  responesCallBack).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"");
    }

    @Override
    public void cancle() {

    }

    public class NetGetHeaderAstask extends AsyncTask {
        String url; Map<String, Object> params; Map<String, String> header; long timeOut; boolean cach; NetCallback responesCallBack;

        public NetGetHeaderAstask(String url, Map<String, Object> params, Map<String, String> header, long timeOut, boolean cach, NetCallback responesCallBack) {
            this.url = url;
            this.params = params;
            this.header = header;
            this.timeOut = timeOut;
            this.cach = cach;
            this.responesCallBack = responesCallBack;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (responesCallBack!=null){
                responesCallBack.onstart();
            }
        }

        @Override
        protected Object doInBackground(Object[] para) {
            url = MapUtils.map2UrlParams(url, params);
            Logger.e("reslut=url=" + url);
            String reslut= HttpUrlConnectionRequest.get(url,header);
            Logger.e("reslut= respones"+reslut);
            System.out.println("net requestUrl " + url);
            System.out.println("net respones "+reslut);

            return reslut;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            if (responesCallBack!=null){
                responesCallBack.onEnd();
            }
            if (o==null|| StringTools.isNullOrEmpty(o.toString())){
                responesCallBack.dealFailer(new Exception());
                return;
            }
            responesCallBack.dealReslut(o.toString());
        }
    }

    public class NetPostHeaderAstask extends AsyncTask {
        String url; Map<String, Object> params; Map<String, String> header; long timeOut; boolean cach; NetCallback responesCallBack;

        public NetPostHeaderAstask(String url, Map<String, Object> params, Map<String, String> header, long timeOut, boolean cach, NetCallback responesCallBack) {
            this.url = url;
            this.params = params;
            this.header = header;
            this.timeOut = timeOut;
            this.cach = cach;
            this.responesCallBack = responesCallBack;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (responesCallBack!=null){
                responesCallBack.onstart();
            }
        }

        @Override
        protected Object doInBackground(Object[] para) {
            Logger.e("reslut=url=" + url);
            String reslut= HttpUrlConnectionRequest.post(url,header,params);
            Logger.e(url+"reslut= respones"+reslut);
            System.out.println("net requestUrl " + url);
            System.out.println("net respones "+reslut);

            return reslut;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            if (responesCallBack!=null){
                responesCallBack.onEnd();
            }
            if (o==null|| StringTools.isNullOrEmpty(o.toString())){
                responesCallBack.dealFailer(new Exception());
                return;
            }
            responesCallBack.dealReslut(o.toString());
        }
    }


}
